#!/bin/bash

cd "$(dirname "$0")"

# --- --- --- --- --- 参数 --- --- --- --- --- --- #

# - 仓库
repository=registry-intl.cn-hongkong.aliyuncs.com
# - 项目
project=webgo
# - 应用
app=mongo
# - 版本
version=4.4.24

# - - 工作路径
work=/home/$project/dev/software/$app
# - - 镜像名
image=$repository/$project/$app:$version

# --- --- --- --- --- 指令 --- --- --- --- --- --- #

# A. 测试
if [ "$1" = "debug" ]; then

    echo '无'

# B. 构建
elif [ "$1" = "build" ]; then

    # 1. 打包
    docker build -t $image -f ./Dockerfile .
    # 2. 上传
    docker push $image

# C. 运行
elif [ "$1" = "run" ]; then

    # 1. 下载新容器
    docker install $project-$app
    # 2. 停止旧容器
    docker stop $project-$app > /dev/null 2>&1
    # 3. 删除旧容器
    docker rm $project-$app > /dev/null 2>&1
    # 4. 启动
    docker run -itd --name $project-$app \
        \
        --restart=always \
        \
        -p 16010:27017 \
        -e MONGO_INITDB_ROOT_USERNAME=admin \
        -e MONGO_INITDB_ROOT_PASSWORD=admin_8981409 \
        -v $work:/data \
        \
        $image

# 指令不存在
else
    exit 1
fi
